package com.yikome.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.yikome.constant.CommonConstant;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;

@Component
public class BoheUtils {

	@Value("${hmb123.basicUrl}")
    private String basicUrl;
	
	@Value("${hmb123.apiToken}")
    private String apiToken;
	
	@Value("${hmb123.uploadPath}")
    private String uploadPath;
	
	@Value("${hmb123.basicDownloadUrl}")
    private String basicDownloadUrl;
	
	public String saveImage(String imgSourceUrl) {
		return imgSourceUrl ; 
	}
	
	public String saveImage(String imageName, InputStream inputStream) {
        
		String imgSuffix = imageName.substring(imageName.lastIndexOf("."));
        String fileName = IdUtil.randomUUID() + imgSuffix;
        
        FileUtil.writeFromStream(inputStream, FileUtil.file(CommonConstant.BASE_LOCATION + fileName));
        
        HttpResponse httpResponse = HttpRequest.post(basicUrl)
				.contentType("application/x-www-form-urlencoded")
				.form(MapUtil.builder(new HashMap<String, Object>())
							.put("api_token", apiToken)
							.put("upload_format", "file")
							.put("uploadedFile", new File(CommonConstant.BASE_LOCATION + fileName))
							.put("mode", 2)
							.put("uploadPath", uploadPath)
							.put("image_compress", 1)
							.put("image_compress_level", 80)
							.build())
				.timeout(10000)
				.execute();

		if(!httpResponse.isOk()) {
			throw new RuntimeException("uploadByUrl http errors");
		}
		if(!StrUtil.equals("200", StrUtil.removeAll(JSONUtils.getJsonNodeString(httpResponse.body(), "statusCode"), "\""))) {
			throw new RuntimeException("uploadByUrl upload errors: " + StrUtil.removeAll(JSONUtils.getJsonNodeString(httpResponse.body(), "resultData"), "\""));
		}
		
		return basicDownloadUrl + uploadPath + fileName ;
    }
	
	public static void main(String[] args) throws IORuntimeException, IOException {
		
		String basicUrl = "https://s1.r379.com/hmb23/api/upload/" ;
		String apiToken = "ff77ba4596106c3dda40" ;
		String uploadPath = DateUtil.date().toString("yyyy/MM/dd");
		
		BufferedInputStream inputStream = FileUtil.getInputStream("D:/data/download/third/511480808.jpg");
		FileUtil.writeFromStream(inputStream, "D:/data/download/third/511480808-1.jpg");
		
		HttpResponse httpResponse = HttpRequest.post(basicUrl)
				.contentType("application/x-www-form-urlencoded")
				.form(MapUtil.builder(new HashMap<String, Object>())
							.put("api_token", apiToken)
							.put("upload_format", "file")
							.put("uploadedFile", FileUtil.file("D:/data/download/third/511480808-1.jpg"))
							.put("mode", 2)
							.put("uploadPath", uploadPath)
							.build())
				.timeout(10000)
				.execute();

		if(!httpResponse.isOk()) {
			throw new RuntimeException("uploadByUrl http errors");
		}
		if(!StrUtil.equals("200", StrUtil.removeAll(JSONUtils.getJsonNodeString(httpResponse.body(), "statusCode"), "\""))) {
			throw new RuntimeException("uploadByUrl upload errors: " + StrUtil.removeAll(JSONUtils.getJsonNodeString(httpResponse.body(), "resultData"), "\""));
		}
	}
	
}
